package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.sky.dto.DishDTO;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.result.Result;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController("AdminSetmealController")
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 套餐分页查询
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize,Integer status,String name,Long categoryId){
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(status!=null,Setmeal::getStatus,status);
        lambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
        lambdaQueryWrapper.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        setmealService.page(pageInfo,lambdaQueryWrapper);
        return Result.success(pageInfo);
    }

    /**
     * 新增套餐
     */
    @ApiOperation("新增套餐")
    @PostMapping
    public Result save(@RequestBody SetmealDTO setmealDTO){
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Random random=new Random();
        Long setmealId=(long) random.nextInt(1000000);
        setmeal.setId(setmealId);
        if(setmealDishes!=null){
            setmealDishes=setmealDishes.stream().map(item->{
                item.setSetmealId(setmealId);
                return item;
            }).collect(Collectors.toList());
            setmealDishService.saveBatch(setmealDishes);
        }
        setmealService.save(setmeal);
        //删除对应缓存
        String key="setmeal_"+ setmealDTO.getCategoryId();
        redisTemplate.delete(key);
        return Result.success("添加套餐成功");
    }

    /**
     * 修改套餐状态
     */
    @ApiOperation("修改套餐状态")
    @PostMapping("/status/{status}")
    public Result<String> changeStatus(@PathVariable Integer status,Integer id){
        Setmeal setmeal = setmealService.getById(id);
        setmeal.setStatus(status);
        setmealService.updateById(setmeal);
        //删除对应缓存
        String key="setmeal_"+ setmeal.getCategoryId();
        redisTemplate.delete(key);
        return Result.success("修改状态成功");
    }

    /**
     * 根据id删除
     */
    @ApiOperation("根据id删除")
    @DeleteMapping
    public Result<String> deleteByIds(Long[] ids){
        ArrayList<Long> list=new ArrayList<>(ids.length);
        Collections.addAll(list,ids);
        setmealService.removeByIds(list);
        //删除套餐对应的菜品
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        list.stream().map((setmealId)->{
            lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealId);
            setmealDishService.remove(lambdaQueryWrapper);
            return setmealId;
        }).collect(Collectors.toList());
        //删除所有setmeal缓存
        cleanCaChe("setmeal_*");
        return Result.success("删除成功");
    }

    /**
     * 根据id查询套餐
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealDTO> getById(@PathVariable Long id){
        SetmealDTO setmealDTO=new SetmealDTO();
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getId,id);
        Setmeal setmeal = setmealService.getOne(setmealLambdaQueryWrapper);
        BeanUtils.copyProperties(setmeal,setmealDTO);
        Long setmealId = setmeal.getId();
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        setmealDTO.setSetmealDishes(list);
        return Result.success(setmealDTO);
    }

    /**
     * 修改套餐内容
     */
    @ApiOperation("修改套餐内容")
    @PutMapping
    public  Result<String> update(@RequestBody SetmealDTO setmealDTO){
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealService.updateById(setmeal);
        //更新相关的菜品
        Long setmealId=setmeal.getId();
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(lambdaQueryWrapper);
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        list=list.stream().map((setmealdish)->{
            setmealdish.setSetmealId(setmealId);
            return setmealdish;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(list);
        //删除所有
        cleanCaChe("setmeal_*");
        return Result.success("修改成功");
    }

    //删除所有pattern类型的缓存
    private void  cleanCaChe(String pattern){
        Set keys = redisTemplate.keys(pattern);
        if(keys!=null){
            redisTemplate.delete(keys);
        }
    }

}
